package com.oceansoft.mobile.econsole.modules.log.controller;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.oceansoft.mobile.econsole.modules.advice.entity.Advice;
import com.oceansoft.mobile.econsole.modules.apps.entity.AppDownload;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.log.service.IDownLogService;
/**
 * 下载日志
 */
@Controller
@RequestMapping("sys/download")
public class DownloadLogController extends BaseController{
	
	@Resource
	private IDownLogService downLogService;
		@RequestMapping(value = "index")
		public String gotoDownLogIndex(Query query, Model model) {
			   Pager pager = downLogService.getDownLogPager(query);
			model.addAttribute("query", query);
			model.addAttribute("pager", pager);

			return "log/download-list";
		}
		
		
		  /**
	     * 统计量导出
	     *
	     * @return
	     */
	    @RequestMapping("exportExcel.xls")
	    public void exportExcel(HttpServletRequest request, HttpServletResponse response, Query query) {
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	      //  String newdate = sdf.format(new Date());
	        // 第一步，创建一个webbook，对应一个Excel文件  
	        HSSFWorkbook wb = new HSSFWorkbook();  
	        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
	        HSSFSheet sheet = wb.createSheet("下载量统计");  
	        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
	        HSSFRow row = sheet.createRow((int) 0);  
	        // 第四步，创建单元格，并设置值表头 设置表头居中  
	        HSSFCellStyle style = wb.createCellStyle();  
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
	  
	        HSSFCell cell = row.createCell((short)0);  
	        cell.setCellValue("下载时间");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short)1);  
	        cell.setCellValue("浏览器类型");  
	        cell.setCellStyle(style);  
	     
	      
	    
	  
	        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，  
	        List<AppDownload> list= downLogService.loadExcel(query); 
	        for (int i = 0; i < list.size(); i++)  
	        {  
	            row = sheet.createRow((int) i + 1);  
	           AppDownload ad = list.get(i);  
	            // 第四步，创建单元格，并设置值  
	        
	           row.createCell((short)0).setCellValue(sdf.format(ad.getCreateTime())); 
	           row.createCell((short)1).setCellValue(ad.getClientBrowser()); 
	        
	        }  
	        // 第六步，将文件存到指定位置  
	        try  
	        {
	        	response.setContentType("application/ms-excel");
	        	OutputStream out = response.getOutputStream();
	        	wb.write(out);  
	            out.close();  
	        }
	        catch (Exception e)  
	        {  
	            e.printStackTrace();  
	        }  
	    }
	    



}
